﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using CodeStash.Common.DataAccess.Repository;
using CodeStash.Common.DataAccess.EntityFramework;

namespace CodeStash.IntegrationTests
{
    [TestFixture]
    public class CodeSnippetTests
    {
        [Test]
        public void SimpleInsertAndDeleteTest()
        {
            Repository<CodeSnippet> codeSnippetRepository = new Repository<CodeSnippet>();

            Repository<Language> languageRepository = new Repository<Language>();
            Repository<CodeCategory> codeCategoryRepository = new Repository<CodeCategory>();
            Repository<Grouping> groupingRepository = new Repository<Grouping>();
            Repository<Visibility> visibilityRepository = new Repository<Visibility>();

                
            
            using (var db = new CodeStashEntities("name=CodeStashEntities"))
            {
                CodeSnippet newCodeSnippet = CreateCodeSnippet(codeSnippetRepository, languageRepository, 
                    codeCategoryRepository, groupingRepository, visibilityRepository, db);
               
                int newlyCreatedCodeSnippetId = newCodeSnippet.CodeSnippetId;
                Assert.Greater(newCodeSnippet.CodeSnippetId,0);

                codeSnippetRepository.Remove(newCodeSnippet);
                db.SaveChanges();

                IEnumerable<CodeSnippet> snippets= codeSnippetRepository
                    .FindBy(x => x.CodeSnippetId == newlyCreatedCodeSnippetId);
                Assert.AreEqual(0, snippets.Count());
            }
        }


        [Test]
        public void SimpleUpdateTest()
        {
            Repository<CodeSnippet> codeSnippetRepository = new Repository<CodeSnippet>();

            Repository<Language> languageRepository = new Repository<Language>();
            Repository<CodeCategory> codeCategoryRepository = new Repository<CodeCategory>();
            Repository<Grouping> groupingRepository = new Repository<Grouping>();
            Repository<Visibility> visibilityRepository = new Repository<Visibility>();



            using (var db = new CodeStashEntities("name=CodeStashEntities"))
            {
                CodeSnippet newCodeSnippet = CreateCodeSnippet(codeSnippetRepository, languageRepository,
                    codeCategoryRepository, groupingRepository, visibilityRepository, db);


                Language newLanguage = languageRepository.FindAll().First();
                Visibility newVisibility = visibilityRepository.FindAll().First();

                CodeCategory newCat = new CodeCategory(Guid.NewGuid().ToString());
                codeCategoryRepository.Add(newCat);
                Grouping newGrouping = new Grouping(Guid.NewGuid().ToString());
                groupingRepository.Add(newGrouping);

                db.SaveChanges();

                Guid newAspNetMembershipUserId = Guid.NewGuid();
                string actualCode = "int i =10;";
                string newDescription = Guid.NewGuid().ToString();
                string newTitle = Guid.NewGuid().ToString();



                newCodeSnippet.ActualCode = actualCode;
                newCodeSnippet.AspNetMembershipUserId = newAspNetMembershipUserId;
                newCodeSnippet.CodeCategory = newCat;
                newCodeSnippet.Grouping = newGrouping;
                newCodeSnippet.Description = newDescription;
                newCodeSnippet.Title = newTitle;
                newCodeSnippet.Language = newLanguage;
                newCodeSnippet.Visibility1 = newVisibility;
                db.SaveChanges();


                Assert.AreEqual(actualCode, newCodeSnippet.ActualCode);
                Assert.AreEqual(newAspNetMembershipUserId, newCodeSnippet.AspNetMembershipUserId);
                Assert.AreEqual(newCat.CodeCategoryName, newCodeSnippet.CodeCategory.CodeCategoryName);
                Assert.AreEqual(newCat.CodeCategoryId, newCodeSnippet.CodeCategoryId);
                Assert.AreEqual(newGrouping.Description, newCodeSnippet.Grouping.Description);
                Assert.AreEqual(newGrouping.GroupId, newCodeSnippet.GroupId);
                Assert.AreEqual(newDescription, newCodeSnippet.Description);
                Assert.AreEqual(newTitle, newCodeSnippet.Title);
                Assert.AreEqual(newLanguage.LanguageCode, newCodeSnippet.Language.LanguageCode);
                Assert.AreEqual(newLanguage.LanguageId, newCodeSnippet.LanguageId);
                Assert.AreEqual(newVisibility.VisibilityDescription, newCodeSnippet.Visibility1.VisibilityDescription);
                Assert.AreEqual(newVisibility.Id, newCodeSnippet.Visibility);
                
            }
        }


        private static CodeSnippet CreateCodeSnippet(Repository<CodeSnippet> codeSnippetRepository, 
            Repository<Language> languageRepository, Repository<CodeCategory> codeCategoryRepository, 
            Repository<Grouping> groupingRepository, Repository<Visibility> visibilityRepository, 
            CodeStashEntities db)
        {
            codeCategoryRepository.EnrolInUnitOfWork(db);
            groupingRepository.EnrolInUnitOfWork(db);
            languageRepository.EnrolInUnitOfWork(db);
            visibilityRepository.EnrolInUnitOfWork(db);
            codeSnippetRepository.EnrolInUnitOfWork(db);


            Guid uniqueId = Guid.NewGuid();

            CodeCategory newCat = new CodeCategory("Cat Name");
            codeCategoryRepository.Add(newCat);

            Grouping newGrouping = new Grouping("Group Desc");
            groupingRepository.Add(newGrouping);

            db.SaveChanges();

            Language lang = languageRepository.FindAll().First();
            Visibility vis = visibilityRepository.FindAll().First();

            CodeSnippet newCodeSnippet = new CodeSnippet("foreach(string s in strings)", newCat.CodeCategoryId,
                "description", "title", newGrouping.GroupId, lang.LanguageId, Guid.NewGuid(), vis, 0,0,0, DateTime.Now);

            codeSnippetRepository.Add(newCodeSnippet);

            db.SaveChanges();
            return newCodeSnippet;
        }

        
    }
}
